package vn.demand.dragon.view
{
	import com.adobe.utils.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.SupplierProxy;
	import vn.demand.dragon.model.vo.Supplier;
	import vn.demand.dragon.view.components.QuickSupplier;;
	
	public class QuickSupplierMediator extends Mediator
	{
		
		public static var NAME:String = "QuickSupplierMediator";
		
		private var masterdataProxy:MasterdataProxy;
		private var supplierProxy:SupplierProxy;
		
		var supplier:Supplier = new Supplier();
		
		public function QuickSupplierMediator(mediatorId:String)
		{
			super(mediatorId);
			masterdataProxy = facade.retrieveProxy(MasterdataProxy.NAME) as MasterdataProxy;
			supplierProxy = facade.retrieveProxy(SupplierProxy.NAME) as SupplierProxy;
		}

		public function get supplierForm():QuickSupplier {
			return viewComponent as QuickSupplier;
		}
		
		public function show(supplierVO:Supplier):void {
			supplier = supplierVO;
			RIALogger.debug(this, "supplier code: " + supplier.sup_code);
			var popup:QuickSupplier = PopUpManager.createPopUp(Application.application as DisplayObject, QuickSupplier, true) as QuickSupplier;
			setViewComponent(popup);
			supplierForm.classification.dataProvider = masterdataProxy.classificationDP;
			supplierForm.type.dataProvider = masterdataProxy.supplierTypesDP;
			supplierForm.sup_status.dataProvider = masterdataProxy.supplierStatusesDP;
			supplierForm.division.dataProvider = masterdataProxy.divisionsDP;
			supplierForm.cost_center.dataProvider = masterdataProxy.costCentersDP;
			supplierForm.resource.dataProvider = masterdataProxy.usersDP;
			
			supplierForm.classification.selectedData = supplier.sup_classification;
			supplierForm.type.selectedData = supplier.sup_type;
			supplierForm.sup_status.selectedData = supplier.sup_status;
			supplierForm.division.selectedData = supplier.sup_division;
			supplierForm.cost_center.selectedData = supplier.sup_costcenter;
			supplierForm.resource.selectedData = supplier.sup_resource;
			supplierForm.code.text = supplier.sup_code;
			supplierForm.supplier_name.text = supplier.sup_name;
			supplierForm.address.text = supplier.sup_address;
			supplierForm.tel.text = supplier.sup_tel;
			supplierForm.fax.text = supplier.sup_fax;
			supplierForm.ledger1.text = supplier.sup_ledger1;
			supplierForm.ledger2.text = supplier.sup_ledger2;
			supplierForm.exact_code.text = supplier.sup_exact_code;
			supplierForm.vat_code.text = supplier.sup_vat_code;
			
			if (supplier.sup_id > 0) {
				supplierForm.title = "Edit supplier: " + supplier.sup_code;
			} else {
				supplierForm.title = "Add supplier";
			}
			
			supplierForm.addEventListener("addSupplier", onAddSupplier);
			supplierForm.addEventListener("cancelAddSupplier", onClose);
			PopUpManager.centerPopUp(popup);
		}
		
		public function onAddSupplier(event:Event):void {
			if (!doValidate()) {
				alert.error(errorStrings);
				return;
			}
			
			supplier.sup_code = supplierForm.code.text;
			supplier.sup_name = supplierForm.supplier_name.text;
			supplier.sup_address = supplierForm.address.text;
			supplier.sup_type = supplierForm.type.selectedData;
			supplier.sup_classification = supplierForm.classification.selectedData;
			supplier.sup_status = supplierForm.sup_status.selectedData;
			supplier.sup_tel = supplierForm.tel.text;
			supplier.sup_fax = supplierForm.fax.text;
			supplier.sup_division = supplierForm.division.selectedData;
			supplier.sup_costcenter = supplierForm.cost_center.selectedData;
			supplier.sup_resource = supplierForm.resource.selectedData;
			supplier.sup_exact_code = supplierForm.exact_code.text;
			supplier.sup_vat_code = supplierForm.vat_code.text;
			supplier.sup_ledger1 = supplierForm.ledger1.text;
			supplier.sup_ledger2 = supplierForm.ledger2.text;
			
			supplierProxy.saveSupplier(supplier);
		}
		
		public function doValidate():Boolean {
			errorStrings = "";
			if (StringUtil.trim(supplierForm.code.text).length == 0) {
				errorStrings += "[Code] is required\n";
			}
			if (StringUtil.trim(supplierForm.supplier_name.text).length == 0) {
				errorStrings += "[Name] is required\n";
			}
			if (StringUtil.trim(supplierForm.ledger1.text).length == 0) {
				errorStrings += "[Ledger 1] is required\n";
			}
			if (StringUtil.trim(supplierForm.ledger2.text).length == 0) {
				errorStrings += "[Ledger 2] is required\n";
			}
			if (supplierForm.sup_status.selectedIndex == 0) {
				errorStrings += "[Status] is required\n";
			}
			if (supplierForm.type.selectedIndex == 0) {
				errorStrings += "[Type] is required\n";
			}
			if (supplierForm.classification.selectedIndex == 0) {
				errorStrings += "[Classification] is required\n";
			}
			return errorStrings.length == 0	? true : false;
		}
		
		public function onClose(event:Event):void {
			if (null != supplierForm) {
				PopUpManager.removePopUp(supplierForm);
			}
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	CommonCommand.SUPPLIER_ADDED,
            	CommonCommand.SUPPLIER_ADD_FAILED,
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				case CommonCommand.SUPPLIER_ADDED:
					onClose(new Event("closeQuickSupplier"));
					break;
				case CommonCommand.SUPPLIER_ADD_FAILED:
					errorStrings = String(note.getBody()) as String;
					alert.error(errorStrings);
                default:
                	break;
            }
        }
        
        private var errorStrings:String;
	}
}